package com.sgb.kjwl.viewmodel.home;

import com.sgb.kjwl.api.APIManager;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.kjwl.model.entity.user.BigHomeAndroidEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.CollectionSummaryFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CollectionSummaryModel extends BaseViewModel {
    public HomeRepertory repertory;
    private CollectionSummaryFragment fragment;
    private int page = 1;
    private Map map;

    public CollectionSummaryModel(CollectionSummaryFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }
    public void getJiCaiList(String firstType, String secondTypeNo, String groupType, String cityNo, String statusNo, String timeIntervals, int groupClass, String allSearchKey, String methodNo, String offerNo, boolean b) {
        map = new HashMap();
        page = b ? page + 1 : 1;
        map.put("limit", 6);
        map.put("current", page);
//        map.put("cityNo", cityNo);//市级编号
//        map.put("districtNo", "");//区编号   --未使用
//        map.put("endDayType", timeIntervals);//报价截止时间：1,0-5天，2，6-10天，3，11-20天，4,20-30天。5，31-50天，6，51-70天，7，71-90天
//        if ("".equals(groupType)) {//集采大厅
        map.put("groupBuyType", 2);//集采方式  0  全部   1.即报即采  2.汇集采购  3.企业专项
//        } else {
//            map.put("groupBuyType", groupType);
//        }
//        map.put("groupbuyClass", groupClass);//集采类型  商品、机械、设备…………
//        map.put("joinEndtime", "");//加盟截止时间   --未使用
//        if("".equals(groupType)){//集采大厅
//            if ("3".equals(methodNo)){//企业专项筛选
//                map.put("offerType", 2);//报价类型
//            }else {
//                map.put("offerType", offerNo);//报价类型
//            }
//        }else {
//            map.put("offerType", offerNo);//报价类型
//        }
//        map.put("order", "");//排序,,1，加盟数量，2，报价数量,3，加盟时间
//        map.put("provinceNo", repertory.getAreaCode());//省编号
//        map.put("searchKey", allSearchKey);//搜索内容
//        map.put("status", statusNo);//状态[1 加盟中 2 报价中 3集采完成
//        map.put("typeNo", firstType);//集采类别
//        if (null != secondTypeNo && !"".equals(secondTypeNo)) {
//            map.put("typeNo", secondTypeNo);//最后一级类目
//        }
//        map.put("createCompany", "");//发起企业
        APIManager.getInstance().getCentralizedMiningAPI().getJiCaiList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> call, Response<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    DialogHelper.dismissProgressDialog();
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showList(response.body().getData().getList());
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void recomandCate(String code) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getBigHomeAPI().recomandCate(repertory.getAreaCode(),code).enqueue(new Callback<BaseEntity<BigHomeAndroidEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<BigHomeAndroidEntity>> call, Response<BaseEntity<BigHomeAndroidEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (code.equals("groupbuy")) {
                        fragment.showOption1View(response.body().getData());
                    } else {
                        MToast.showToast(fragment.getActivity(), response.message());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<BigHomeAndroidEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("collection").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(fragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = response.body().getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + repertory.getToken();
//                    String url = "http://192.168.2.33:8067/collection" + "?id=" + id + "&type=0"+"&token="+ repertory.getToken();
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
